﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVGoods : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditKTVGoods()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InitRoomType()
        {
            List<KTVGoodsTypeInfo> list = BLLFactory<KTVGoodsType>.Instance.GetAll();
            this.cmbGoodsType.Items.Clear();
            foreach (KTVGoodsTypeInfo typeInfo in list)
            {
                CListItem item = new CListItem(typeInfo.GoodsType, typeInfo.GoodsType);
                this.cmbGoodsType.Items.Add(item);
            }
        }

        private void FrmEditRoomType_Load(object sender, EventArgs e)
        {
            InitRoomType();

            string chkNotClose = RegistryHelper.GetValue(Constants.IS_GOODS_EDIT_OPEN);
            if (!string.IsNullOrEmpty(chkNotClose))
            {
                this.chkNotClose.Checked = (chkNotClose == "0") ? false : true;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                KTVGoodsInfo info = BLLFactory<KTVGoods>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.txtAlarm.Text = info.AlarmQuantity.ToString();
                    this.txtItemNo.Text = info.ItemNo;
                    this.txtName.Text = info.Name;
                    this.txtPrice.Text = info.Price.ToString("f2");
                    this.txtStock.Text = info.StockQuantity.ToString();
                    this.txtUnitCost.Text = info.UnitCost.ToString("f2");
                    this.cmbUnitName.Text = info.UnitName;
                    this.chkFlag.Checked = info.Flag;
                    this.cmbGoodsType.Text = info.GoodsType;
                    this.txtNote.Text = info.Note;
                    this.txtDuralDays.Text = info.DuralDays.ToString();
                }
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                //this.btnOK.Enabled = Portal.gc.HasFunction("Product/Add");
            }
            this.cmbGoodsType.Focus();
        }

        private void SetInfo(KTVGoodsInfo info)
        {
            info.AlarmQuantity = Convert.ToInt32(this.txtAlarm.Text);
            info.Flag = this.chkFlag.Checked;
            info.GoodsType = this.cmbGoodsType.Text;
            info.ItemNo = this.txtItemNo.Text;
            info.Name = this.txtName.Text;
            info.Note = this.txtNote.Text;
            info.Price = Convert.ToDecimal(this.txtPrice.Text);
            info.StockQuantity = Convert.ToInt32(this.txtStock.Text);
            info.UnitCost = Convert.ToDecimal(this.txtUnitCost.Text);
            info.UnitName = this.cmbUnitName.Text;
            info.DuralDays = Convert.ToDouble(this.txtDuralDays.Text);
        }

        private void ClearInput()
        {
            this.ID = "";
            this.cmbUnitName.Text = "";
            this.txtAlarm.Text = "0";
            this.txtItemNo.Clear();
            this.txtNote.Clear();
            this.txtName.Clear();
            this.txtPrice.Text = "0.00";
            this.txtStock.Text = "0";
            this.txtUnitCost.Text = "0.00";
            this.chkFlag.Checked = false;
            this.txtDuralDays.Text = "0";

            this.Text = this.Text.Replace("编辑", "新建");
            this.cmbGoodsType.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 检查
            if (this.cmbGoodsType.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入商品类别");
                this.cmbGoodsType.Focus();
                return;
            }
            if (this.txtItemNo.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入项目编码");
                this.txtItemNo.Focus();
                return;
            }
            if (this.txtName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请输入项目名称");
                this.txtName.Focus();
                return;
            } 
            #endregion

            if (!string.IsNullOrEmpty(ID))
            {
                KTVGoodsInfo info = BLLFactory<KTVGoods>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<KTVGoods>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            if (this.chkNotClose.Checked)
                            {
                                ClearInput();
                            }
                            else
                            {
                                this.DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                KTVGoodsInfo info = new KTVGoodsInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<KTVGoods>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        if (this.chkNotClose.Checked)
                        {
                            ClearInput();
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void chkNotClose_CheckedChanged(object sender, EventArgs e)
        {
            //保存注册表，记录下来
            RegistryHelper.SaveValue(Constants.IS_GOODS_EDIT_OPEN, this.chkNotClose.Checked ? "1" : "0");
        }
    }
}
